package com.ylkj.patient.rnmodule;

import com.alibaba.fastjson.JSON;
import com.example.messagemodule.utils.MessageUtils;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableMap;
import com.users.rn.kit.bridge.impl.im.KitImModule;
import com.examination.mlib.interfaceapi.MessageApi;
import com.yilijk.base.utils.ALog;
import com.ylkj.patient.utils.AppReactUtils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

public class RNKitImModule extends KitImModule {

    private static final String TAG = "IMMessage";
    private HashMap<String, Object> messageMap;

    /**
     * 发送消息
     *
     * @param reactContext
     * @param readableMap
     * @param promise
     */
    @Override
    public void sendMessage(@NotNull ReactContext reactContext, @NotNull ReadableMap readableMap, @NotNull Promise promise) {

        //接收数据取值
        String datajson = AppReactUtils.INSTANCE.delNativeMap(readableMap.toString());
        ALog.d(TAG, "sendMessage: " + datajson);
        try {
            MessageUtils.sendImMsg(reactContext, datajson, new MessageApi() {
                @Override
                public void onSuccess(String data) {
                    promise.resolve(data);
                }

                @Override
                public void onError(String failInfo) {
                    promise.resolve(failInfo);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            promise.reject(e);
        }
    }

    /**
     * 获取漫游数据
     *
     * @param reactContext
     * @param readableMap
     * @param promise
     */
    @Override
    public void getRoamingMessageList(@NotNull ReactContext reactContext, @NotNull ReadableMap readableMap, @NotNull Promise promise) {
        ALog.d(TAG, "recallMessage: " + readableMap);
        String nativeMap = AppReactUtils.INSTANCE.delNativeMap(readableMap.toString());
        Map map = (Map) JSON.parse(nativeMap);
        String to = (String) map.get("to");//a3
        int pageSize = (Integer) map.get("size");//15
        String messageId = (String) map.get("messageId");//15
        ALog.d(TAG, "getRoamingmessagebody: " + to);
        ALog.d(TAG, "getRoamingmessagebTHody: " + pageSize);
        String imHistoryMsgJSON = MessageUtils.getImHistoryMsgJSON(to, 0, pageSize, null);
        promise.resolve(imHistoryMsgJSON);
    }

}
